package com.winning.pmph.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.winning.pmph.entity.Journal;
import com.winning.pmph.entity.KnowledgeLib;
import com.winning.pmph.entity.Platform;
import com.winning.pmph.entity.PlatformKnowledgeLib;
import com.winning.pmph.mapper.PlatformKnowledgeLibMapper;
import com.winning.pmph.service.JournalService;
import com.winning.pmph.service.KnowledgeLibService;
import com.winning.pmph.service.PlatformService;
import com.winning.pmph.utils.PMPHAppUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(value = "系统管理/应用平台管理")
@Slf4j
@RestController
@RequestMapping("/pmph/action/platform")
public class PlatformCtrl {

    @Resource
    PlatformService platformService;
    @Resource
    PlatformKnowledgeLibMapper platformKnowledgeLibMapper;
    @Resource
    KnowledgeLibService knowledgeLibService;
    @Resource
    JournalService journalService;

    @ApiOperation(value = "selectPlatforms", notes = "查询平台列表")
    @GetMapping("selectPlatforms")
    public List<Platform> selectPlatforms() {
        List<Platform> platforms = platformService.lambdaQuery().orderByDesc(Platform::getUpdatedTime)
                .orderByAsc(Platform::getId).list();
        platforms.forEach(platform -> {
            List<String> knowledgeLidIdList = new ArrayList<>();
            List<String> knowledgeLidNameList = new ArrayList<>();
            List<PlatformKnowledgeLib> platformKnowledgeLibs = platformKnowledgeLibMapper.lambdaQuery().eq(PlatformKnowledgeLib::getPlatformId, platform.getId()).list();
            List<String> knowledgeLibIds = platformKnowledgeLibs.stream().map(PlatformKnowledgeLib::getKnowledgeLibId).collect(Collectors.toList());
            List<KnowledgeLib> knowledgeLibs = null;
            List<Journal> journals = null;
            if (CollectionUtils.isNotEmpty(knowledgeLibIds)) {
                knowledgeLibs = knowledgeLibService.listByIds(knowledgeLibIds);
                journals = journalService.lambdaQuery().in(Journal::getIssn, knowledgeLibIds).list();
            }
            if (CollectionUtils.isNotEmpty(journals)) {
                knowledgeLidIdList.add(journals.get(0).getId());
                knowledgeLidNameList.add(journals.get(0).getName());
            }
            platformKnowledgeLibs.stream().forEach(platformKnowledgeLib -> {
                if (platformKnowledgeLib.getSystemType().equals("book")) {
                    knowledgeLidIdList.add("book_lib_id");
                    knowledgeLidNameList.add("图书");
                }
                if (platformKnowledgeLib.getSystemType().equals("calc")) {
                    knowledgeLidIdList.add("flow_chart_lib_id");
                    knowledgeLidNameList.add("医学计算器");
                }
                if (platformKnowledgeLib.getSystemType().equals("flowchart")) {
                    knowledgeLidIdList.add("flow_chart_lib_id");
                    knowledgeLidNameList.add("流程图");
                }
                if (platformKnowledgeLib.getSystemType().equals("term")) {
                    knowledgeLidIdList.add("term_lib_id");
                    knowledgeLidNameList.add("术语");
                }
            });
            if (CollectionUtils.isNotEmpty(knowledgeLibs)) {
                knowledgeLidIdList.add(knowledgeLibs.stream().map(KnowledgeLib::getId).collect(Collectors.joining(",")));
                knowledgeLidNameList.add(knowledgeLibs.stream().map(KnowledgeLib::getName).collect(Collectors.joining(",")));
            }
            platform.setKnowledgeLibIds(knowledgeLidIdList.stream().map(String::toString).collect(Collectors.joining(",")));
            platform.setKnowledgeLibNames(knowledgeLidNameList.stream().map(String::toString).collect(Collectors.joining(",")));
        });
        return platforms;
    }

    @ApiOperation(value = "updatePlatform", notes = "修改平台列表状态接口")
    @PostMapping("updatePlatform")
    public void updatePlatform(String platform) {
        Platform platformJson = JSON.parseObject(platform, Platform.class);
        platformService.savePlatform(platformJson);
    }

    @ApiOperation(value = "insertPlatform", notes = "创建平台接口")
    @PostMapping("insertPlatform")
    public void insertPlatform(String platform) {
        Platform platformJson = JSON.parseObject(platform, Platform.class);
        platformJson.setId(platformJson.getPlatformId());
        platformService.saveOrUpdate(platformJson);
    }

    @ApiOperation(value = "deletePlatform", notes = "删除平台接口")
    @PostMapping("deletePlatform")
    public void deletePlatform(String id) {
        platformService.deletePlatformById(id);
    }

    @ApiOperation(value = "selectKnowledgeLibListByPlatformId", notes = "查看平台绑定知识库列表")
    @GetMapping("selectKnowledgeLibListByPlatformId")
    public List<PlatformKnowledgeLib> selectKnowledgeLibListByPlatformId(String id) {
        List<PlatformKnowledgeLib> platformKnowledgeLibs = platformKnowledgeLibMapper.executeMultiSelect(
                "select * from (SELECT " +
                        "pkl.*, " +
                        "kl.NAME knowledge_lib_name " +
                        "FROM " +
                        "platform_knowledge_lib pkl " +
                        "INNER JOIN knowledge_lib kl ON pkl.KNOWLEDGE_LIB_ID = kl.ID " +
                        "WHERE " +
                        "pkl.PLATFORM_ID = '" + id + "' UNION " +
                        "SELECT " +
                        "pkl.*, " +
                        "j.NAME knowledge_lib_name " +
                        "FROM " +
                        "platform_knowledge_lib pkl " +
                        "INNER JOIN journal j ON pkl.KNOWLEDGE_LIB_ID = j.issn " +
                        "WHERE " +
                        "pkl.PLATFORM_ID = '" + id + "' UNION " +
                        "SELECT " +
                        "pkl.*, " +
                        "'医学计算器' knowledge_lib_name " +
                        "FROM " +
                        "platform_knowledge_lib pkl " +
                        "WHERE " +
                        "pkl.KNOWLEDGE_LIB_ID = 'calculator_lib_id' " +
                        "AND pkl.PLATFORM_ID = '" + id + "' union " +
                        "SELECT " +
                        "pkl.*, " +
                        "'图书' knowledge_lib_name " +
                        "FROM " +
                        "platform_knowledge_lib pkl " +
                        "WHERE " +
                        "pkl.KNOWLEDGE_LIB_ID = 'book_lib_id' " +
                        "AND pkl.PLATFORM_ID = '" + id + "' union " +
                        "SELECT " +
                        "pkl.*, " +
                        "'术语' knowledge_lib_name " +
                        "FROM " +
                        "platform_knowledge_lib pkl " +
                        "WHERE " +
                        "pkl.KNOWLEDGE_LIB_ID = 'term_lib_id' " +
                        "AND pkl.PLATFORM_ID = '" + id + "' union " +
                        "SELECT " +
                        "pkl.*, " +
                        "'流程图' knowledge_lib_name " +
                        "FROM " +
                        "platform_knowledge_lib pkl " +
                        "WHERE " +
                        "pkl.KNOWLEDGE_LIB_ID = 'flow_chart_lib_id' " +
                        "AND pkl.PLATFORM_ID = '" + id + "'" +
                        ") a order by a.updated_time desc");
        return platformKnowledgeLibs;
    }

    @ApiOperation(value = "updatePlatformKnowledgeLib", notes = "修改平台绑定知识库关系")
    @PostMapping("updatePlatformKnowledgeLib")
    public void updatePlatformKnowledgeLib(String platformKnowledgeLib) {
        PlatformKnowledgeLib platformKnowledgeLibs = JSON.parseObject(platformKnowledgeLib, PlatformKnowledgeLib.class);
        List<PlatformKnowledgeLib> platformKnowledgeLibList = Lists.newArrayList();
        platformKnowledgeLibList.add(platformKnowledgeLibs);
        platformService.savePlatformKnowledgeLibs(platformKnowledgeLibList, PMPHAppUtil.getCurrentUserEnName());
    }

    @ApiOperation(value = "deletePlatformKnowledgeLib", notes = "删除平台绑定知识库关系")
    @PostMapping("deletePlatformKnowledgeLib")
    public void deletePlatformKnowledgeLib(String platformKnowledgeLib) {
        PlatformKnowledgeLib platformKnowledgeLibJson = JSONObject.parseObject(platformKnowledgeLib, PlatformKnowledgeLib.class);
        platformService.deletePlatformKnowledgeLibById(platformKnowledgeLibJson);
    }

    @ApiOperation(value = "insertPlatformKnowledgeLibs", notes = "新增平台绑定知识库关系")
    @PostMapping("insertPlatformKnowledgeLibs")
    public void insertPlatformKnowledgeLibs(String platformKnowledgeLibs) {
        List<PlatformKnowledgeLib> platformKnowledgeLibList = JSONArray.parseArray(platformKnowledgeLibs, PlatformKnowledgeLib.class);
        platformService.savePlatformKnowledgeLibs(platformKnowledgeLibList, PMPHAppUtil.getCurrentUserEnName());
    }
}
